package com.jade.editor.anisotropy.node;

import java.util.ArrayList;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.wb.swt.SWTResourceManager;

public class Cortege {

	private ArrayList<Point> _plist = new ArrayList<Point>();
	private Node _fstNode = null;
	private Node _lstNode = null;
	
	private RGB _color = new RGB(0,0,0);
	
	public void setColor(RGB color){
		_color = color;
	}
	
	public Cortege(){
		
	}
	
	public Cortege(Node fstNode, Node lstNode){
		_fstNode = fstNode;
		_fstNode.addCortege(this);
		_lstNode = lstNode;
		_lstNode.addCortege(this);
		fillPoints();
	}
	
	public void init(Composite parent, Collector collector, RGB color, RGB colorSelect){
		_fstNode.init(parent, collector, color, colorSelect);
		_lstNode.init(parent, collector, color, colorSelect);
	}
	
	public ArrayList<Point> getPointsList(){
		return _plist;
	}
	
	public void addFirstNode(Node n){
		_fstNode = n;
		_fstNode.addCortege(this);
		fillPoints();
	}
	
	public void addLastNode(Node n){
		_lstNode = n;
		_lstNode.addCortege(this);
		fillPoints();
	}
	
	public Node getFirstNode(){
		return _fstNode;
	}
	
	public Node getLastNode(){
		return _lstNode;
	}
	
	public boolean hasFirstNode(){
		if (_fstNode != null)
			return true;
		return false;
	}
	
	public boolean hasLastNode(){
		if (_lstNode != null)
			return true;
		return false;
	}
	
	public boolean hasNode(Node n){
		if(n.equals(_fstNode))
			return true;
		if(n.equals(_lstNode))
			return true;
		return false;
	}
	
	public Node getAnotherNode(Node n){
		if(n.equals(_fstNode)){
			return _lstNode;
		}
		if(n.equals(_lstNode)){
			return _fstNode;
		}
		return null;
	}
	
	public void removeNode(Node n){
		if (_fstNode.equals(n)){
			_fstNode = null;
			_plist.clear();
		}
		if (_lstNode.equals(n)){
			_lstNode = null;
			_plist.clear();
		}
	}
	
	public void reCulc(){
		fillPoints();
	}
	
	private void fillPoints(){
		if (_fstNode != null && _lstNode != null){
			_plist.clear();
			
			
			int x1 = _fstNode.x;
			int y1 = _fstNode.y;
			
			int x2 = _lstNode.x;
			int y2 = _lstNode.y;
			
			int d = 2;
			
			double length = Math.sqrt(Math.pow((x2-x1), 2) + Math.pow(y2 - y1, 2));
			int count = (int) (length / d);
			
			
			_plist.add(new Point(_fstNode.x, _fstNode.y));
			
			for (int i = 0; i < count; i++){
				double x3 = x1 + d * (x2 - x1) / length;  
				double y3 = y1 + d * (y2 - y1) / length;  
				
				int xx = (int) x3;
				int yy = (int) y3;
				
				_plist.add(new Point(xx, yy));
				
				d+=2;
			}
			
			if(_plist.get(_plist.size() - 1).x != _lstNode.x && _plist.get(_plist.size() - 1).y != _lstNode.y)
				_plist.add(new Point(_lstNode.x, _lstNode.y));

		}
	}
	
	public void paint(GC gc){
		gc.setForeground(SWTResourceManager.getColor(_color));
		if (_fstNode != null && _lstNode != null){
			gc.drawLine(_fstNode.x, _fstNode.y, _lstNode.x, _lstNode.y);
		}
	}
}
